package com.dhcc.core.framework.security.filter;

import org.pac4j.core.redirect.RedirectAction;
import org.pac4j.core.context.WebContext;
import org.pac4j.core.logout.LogoutActionBuilder;
import org.pac4j.core.profile.CommonProfile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.hutool.core.util.StrUtil;

import com.dhcc.core.config.properties.SsoProperties;

/**
 * logout action builder.
 *
 * @author cyf
 */
public class Pac4jOauth2LogoutActionBuilder<U extends CommonProfile> implements LogoutActionBuilder<U> {

    private static final Logger logger = LoggerFactory.getLogger(Pac4jOauth2LogoutActionBuilder.class);

    @Override
    public RedirectAction getLogoutAction(final WebContext context, final U currentProfile, final String targetUrl) {
        String redirectUrl = SsoProperties.me().getDynamicServerUrl() + "/logout";
        redirectUrl = StrUtil.replace(redirectUrl, "/oauth2.0", "") + "?service=" + SsoProperties.me().getDynamicClientUrl();
        logger.debug("redirectUrl: {}", redirectUrl);
        return RedirectAction.redirect(redirectUrl);
    }

}
